
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8" />
    <title>会员列表-WeAdmin Frame型后台管理系统-WeAdmin 1.0</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0" />
    <link rel="stylesheet" href="../../static/css/font.css" />
    <link rel="stylesheet" href="../../static/css/weadmin.css" />
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="weadmin-body">
    <div class="searchTable">
        搜索ID：
        <div class="layui-inline">
            <input class="layui-input" name="keyword" id="keyword" autocomplete="off">
        </div>
        <button class="layui-btn" data-type="search">搜索</button>
        <a class="layui-btn layui-btn-sm" style="margin-top:3px;float:right;padding: 0 20px;" href="javascript:void('0');"
           title="刷新" data-type="refresh">
            <i class="layui-icon layui-icon-refresh"></i>
        </a>
    </div>
    <table id="list" lay-filter="list"></table>

</div>
<script src="../../static/layui/layui.js" charset="utf-8"></script>
<script>
    layui.extend({
        admin: '{/}../../static/js/admin'
    });
    var sortObj={field:'id',type:'asc'};
    layui.use(['table','admin'], function(){
        var $ = layui.jquery ;
        var table = layui.table;
        table.render({
            elem:"#list"
            ,autoSort:false
            ,url:"/admin/getAdminList"
            ,cols:[[
                {checkbox:true}
                ,{type:'numbers',title:'序号'}
                ,{field:'id',title:'id',sort:true}
                ,{field:'account',title:'账号',sort:true}
                ,{fixed: 'right', align:'center', toolbar: '#handle'}
            ]]
            ,page:true
            ,limits : [10,20,30,40,50,60,70,80,90]
            ,defaultToolbar: ['filter'/*, 'print', 'exports'*/]
            ,height: 'full-200'
            ,toolbar : '#all-handle'
            ,loading : true
            ,title : "管理员"
            ,text : {none:"暂无数据"}
            ,response:{statusCode:200}
        });
        //排序
        table.on('sort(list)',function (obj) {
            sortObj = obj;
            reload(1);
        });
        table.on('toolbar(list)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'add':
                    WeAdminShow('添加管理员','./add.html',600,400)
                    break;
                case 'delete':
                    if (checkStatus.data.length > 0) {
                        var adminIds = checkStatus.data[0].id;
                        if (checkStatus.data.length > 1) {
                            for (var i = 1; i < checkStatus.data.length; i++) {
                                adminIds += ","+checkStatus.data[i].id;
                            }
                        }
                        del(adminIds);
                    }
                    break;
            };
        });
        //监听工具条
        table.on('tool(list)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if(layEvent === 'edit'){ //修改
                $.ajax({
                    type: "get",
                    dataType: "json",
                    url: "/admin/"+data.id+"",
                    contentType: "application/json;charset=UTF-8",//指定消息请求类型
                    data:  {},
                    success: function (data) {
                        if(data.code == null || data.code != 200){
                            layer.msg(data.msg);
                        }else{
                            WeAdminEdit('修改账号','./edit.html',data.data,600,400);
                        }
                    }
                });
                //do somehing
            } else if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    del(data.id);
                    layer.close(index);

                });
            }else if(layEvent === 'editPassword'){ //修改密码
                editPassword(data.id);
            }else if(layEvent === 'editRole'){ //修改密码
                editRole(data.id);
            }
        });

        function editRole(id){
            $.ajax({
                type: "get",
                dataType: "json",
                url: "/adminRole/"+id,
                contentType: "application/json;charset=UTF-8",//指定消息请求类型
                data:  {},
                success: function (data) {
                    if(data.code == null || data.code != 200){
                        layer.msg(data.msg);
                    }else{
                        WeAdminEdit('修改角色','./adminRole.html',data.data,600);
                    }
                }
            });
        }
        function editPassword(id){
            $.ajax({
                type: "get",
                dataType: "json",
                url: "/admin/"+id+"",
                contentType: "application/json;charset=UTF-8",//指定消息请求类型
                data:  {},
                success: function (data) {
                    if(data.code == null || data.code != 200){
                        layer.msg(data.msg);
                    }else{
                        WeAdminEdit('修改密码','./editPassword.html',data.data,600,400);
                    }
                }
            });
        }

        function del(adminIds) {
            $.ajax({
                type: "post",
                dataType: "json",
                url: "/admin/delete",
                contentType: "application/json;charset=UTF-8",//指定消息请求类型
                data:  JSON.stringify(adminIds),
                success: function (data) {
                    if(data.code == null || data.code != 200){
                        layer.msg(data.msg);
                    }else{
                        layer.alert("删除成功", {
                            icon: 6
                        });
                        table.reload('list');
                    }
                }
            });
        };
        var active = {
            search:function(){
                reload(1);
            },
            refresh:function () {
                reload(0);
            }
        }
        $(".searchTable .layui-btn").on('click',function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
        function reload(curr){
            var keyword = $("#keyword");

            if(curr >= 0){
                table.reload('list',{

                    page:{
                        curr: curr //重新从第 1 页开始
                    }
                    ,where: {
                        keyword: keyword.val()
                        ,field: sortObj.field //排序字段
                        ,order: sortObj.type //排序方式
                    }
                    ,initSort:sortObj

                });
            }else{
                table.reload('list',{where: {
                        keyword: keyword.val()
                        ,field: sortObj.field //排序字段
                        ,order: sortObj.type //排序方式
                    }
                    ,initSort:sortObj
                });
            }

        }
    });
</script>
<script type="text/html" id="all-handle">
    <a class="layui-btn layui-btn-xs" lay-event="add">添加</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete">删除</a>
</script>
<script type="text/html" id="handle">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改账号</a>
    <a class="layui-btn layui-btn-xs" lay-event="editPassword">修改密码</a>
    <a class="layui-btn layui-btn-xs" lay-event="editRole">修改角色</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
</body>
</html>
